package org.spindle.dungeon;

import org.newdawn.slick.*;
import org.spindle.dungeon.entities.GeneralEntity;
import org.spindle.dungeon.entities.Player;
import org.spindle.dungeon.entities.WallEntity;
import org.spindle.dungeon.entities.Worm;
import org.spindle.dungeon.rooms.LeverRoom;
import org.spindle.dungeon.rooms.SokoRoom;

public class Dungeon extends BasicGame {

    private LevelProperties props = new LevelProperties();

    public Dungeon() {
        super("Hello World");
    }

    @Override
    public void init(GameContainer gc) throws SlickException {
        try {
            Constants.SPRITE_SHEET = new SpriteSheet("resources/images/sprites2.png", Constants.TILE_SIZE, Constants.TILE_SIZE);
            Player player = new Player();
            player.x = 100;
            player.y = 100;
            GeneralEntity ent;
            for (int i = 0; i < 10; i++) {
                ent = new Worm();
                ent.x = (int) (20 + Math.round(Math.random() * Constants.STAGE_WIDTH));
                ent.y = (int) (20 + Math.round(Math.random() * Constants.STAGE_HEIGHT));
                //.add(ent);
                EntityManager.getInstance().registerEntity(ent);
            }

            new LeverRoom(32, 32, 3);


            EntityManager.getInstance().registerEntity(player);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Shit!");
        }
    }

    @Override
    public void update(GameContainer gc, int delta) throws SlickException {
        EntityManager.getInstance().addFlag = false;
        for (GeneralEntity ent : EntityManager.getInstance().getEntities()) {
            ent.update(gc, props);
            if (ent.lifeTime == 0) {
                EntityManager.getInstance().removeEntity(ent);
            }
        }
        EntityManager.getInstance().processAdditions();

        Input input = gc.getInput();

        if (input.isKeyPressed(Input.KEY_D)) {
            Constants.debug = !Constants.debug;
        }
    }

    @Override
    public void render(GameContainer gc, Graphics g) throws SlickException {
        g.scale(2, 2);

        for (GeneralEntity ent : EntityManager.getInstance().getEntities()) {
            ent.render(props, g);
        }
    }

    public static void main(String[] args) throws SlickException {
        AppGameContainer app = new AppGameContainer(new Dungeon());
        app.setMinimumLogicUpdateInterval(15);
        app.setDisplayMode(1024, 768, false);
        app.start();
    }
}